import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class TestLoader {
   
	public Test loadTest(){
		Token a = new Token("/gallery/m&m.jpg");
		Token b = new Token("gallery/skittle.jpg");
		Test test = new Test();
		test.setReward(a);
		test.setReward(b);
		test.addSession(loadPhaseOne());
		test.addSession(loadPhaseTwo());
		return test;
	}
	
	public Session loadPhaseOne(){
		Session test = new Session(true);
		//Problem is initialized with true or false for delayed and match to sample.
		test.addProblem(true);
		test.addProblemReinforcer(0, 0);
		test.addProblemComparison(0, "/gallery/grizzly panda.jpg", true);
		test.addProblemComparison(0, "/gallery/m_1021332b.jpg", false);
		test.getProblem(0).setSample("/gallery/grizzly panda.jpg");
		test.addProblem(false);
		test.addProblemReinforcer(1, 0);
		test.addProblemComparison(1, "/gallery/animals_cats_small_cat_005241_.jpg", true);
		test.addProblemComparison(1, "/gallery/circle.jpg", false);
		test.addProblemComparison(1, "/gallery/cross arrow.jpg", false);
		test.getProblem(1).setSample("/gallery/cross arrow.jpg");
		test.addProblem(true);
		test.addProblemReinforcer(2, 1);
		test.addProblemComparison(2, "/gallery/puppy-running.jpg", true);
		test.addProblemComparison(2, "/gallery/grizzly panda.jpg", false);
		test.addProblemComparison(2, "gallery/skittle.jpg", false);
		test.getProblem(2).setSample("/gallery/grizzly panda.jpg");
		test.addProblem(false);
		test.addProblemReinforcer(3, 1);
		test.addProblemComparison(3, "/gallery/m_1021332b.jpg", true);
		test.addProblemComparison(3, "/gallery/grizzly panda.jpg", false);
		test.addProblemComparison(3, "/gallery/grizzly panda.jpg", false);
		test.getProblem(3).setSample("/gallery/m_1021332b.jpg");
		return test;
	}
	
   public Test loadTest(String path) throws ParserConfigurationException, SAXException, IOException{
	   Test test = new Test();
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();

       //Here we do the actual parsing
       Document doc = builder.parse(new File(path));

       //Here we get the root element of XML and print out
       //the value of its 'testAttr' attribute
       Element rootElement = doc.getDocumentElement();
       NodeList comparisonDelay = rootElement.getElementsByTagName("comparisondelay");
       NodeList answerDelay = rootElement.getElementsByTagName("answerdelay");
       test.setComparisonDelay(Integer.parseInt(((Element)comparisonDelay.item(0)).getTextContent()));
       test.setAnswerDelay(Integer.parseInt(((Element)answerDelay.item(0)).getTextContent()));
       
       NodeList sessions = rootElement.getElementsByTagName("session");
       for (int i = 0; i < sessions.getLength(); i++){
    	   Element session = (Element) sessions.item(i);
    	   boolean bisRewardedPhase = false;
    	   NodeList phase = session.getElementsByTagName("phase");
    	   NodeList progressBar = session.getElementsByTagName("progressBar");
    	   NodeList sessionName = session.getElementsByTagName("sessionName");
    	   NodeList sessionType = session.getElementsByTagName("sessionType");
    	   if (phase.item(0).getTextContent().equals("true")){
    		   bisRewardedPhase = true;
    	   }
    	   test.addSession(new Session(bisRewardedPhase));
    	   if (progressBar.item(0).getTextContent().equals("true")){
    		   test.getSession(i).setProgressBar(true);
    	   }
    	   test.getSession(i).setName(sessionName.item(0).getTextContent());
    	   test.getSession(i).setSessionType(sessionType.item(0).getTextContent());
    	   NodeList problems = session.getElementsByTagName("problem");
    	   for (int j = 0; j < problems.getLength(); j++){
    		   Element problem = (Element) problems.item(j);
    		   test.getSession(i).addProblem(false);
    		   NodeList sample = problem.getElementsByTagName("sampleImage");
    		   NodeList reinforcer = problem.getElementsByTagName("reinforcer");
    		   NodeList MTS = problem.getElementsByTagName("MTS");
    		   test.getSession(i).getProblem(j).setSample(sample.item(0).getTextContent());
               test.getSession(i).getProblem(j).setReinforcer(Integer.parseInt(reinforcer.item(0).getTextContent()));

    		   if (MTS.item(0).getTextContent().equals("true")){
    			   test.getSession(i).getProblem(j).setMTS(true);
    		   }
    		   NodeList comparisons = problem.getElementsByTagName("comparison");
    		   for (int k = 0; k < comparisons.getLength(); k++){
    			   Element comparison = (Element) comparisons.item(k);
    			   NodeList image = comparison.getElementsByTagName("image");
    			   if (comparison.getAttribute("answer").equals("true")){
    				   test.getSession(i).getProblem(j).addComparison(image.item(0).getTextContent(), true);
    			   } else if (comparison.getAttribute("answer").equals("false")){
    				   test.getSession(i).getProblem(j).addComparison(image.item(0).getTextContent(), false);
    			   }
    		   }
    	   }
       }
       NodeList tokenBoard = rootElement.getElementsByTagName("tokenBoard");
       NodeList tokens = ((Element)tokenBoard.item(0)).getElementsByTagName("token");
       for (int i = 0; i < tokens.getLength(); i++){
    	   test.getTokenBoard().setToken(((Element)tokens.item(i)).getAttribute("image"));
       }
       return test;
   }

	public Session loadPhaseTwo(){
		Session test = new Session(true);
		//Problem is initialized with true or false for delayed and match to sample.
		test.addProblem(false);
		test.addProblemReinforcer(0, 1);
		test.addProblemComparison(0, "/gallery/double arrow.jpg", true);
		test.addProblemComparison(0, "/gallery/m_1021332b.jpg", false);
		test.getProblem(0).setSample("/gallery/cross arrow.jpg");
		test.addProblem(true);
		test.addProblemReinforcer(1, 1);
		test.addProblemComparison(1, "/gallery/cross arrow.jpg", true);	
		test.addProblemComparison(1, "/gallery/puppy-dog.jpg", false);
		test.addProblemComparison(1, "/gallery/grizzly panda.jpg", false);
		test.getProblem(1).setSample("/gallery/double arrow.jpg");
		test.addProblem(true);
		test.addProblemReinforcer(2, 0);
		test.addProblemComparison(2, "/gallery/swirly.jpg", true);
		test.addProblemComparison(2, "/gallery/grizzly panda.jpg", false);
		test.addProblemComparison(2, "gallery/how-to-solve-cat-behavior-problems-2.jpg", false);
		test.getProblem(2).setSample("/gallery/zigzag.jpg");
		test.addProblem(false);
		test.addProblemReinforcer(3, 0);
		test.addProblemComparison(3, "/gallery/zigzag.jpg", true);
		test.addProblemComparison(3, "/gallery/grizzly panda.jpg", false);
		test.addProblemComparison(3, "/gallery/grizzly panda.jpg", false);
		test.getProblem(3).setSample("/gallery/swirly.jpg");
		return test;
	}
}

